package szf.im.server.hander;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import szf.im.server.data.MessageOuterClass;
import szf.im.server.data.OnlineHelp;
import szf.im.server.data.ResultWrapper;

/**
 * @author shizhanfu
 */
public class ProtoBufMsgHandler extends SimpleChannelInboundHandler<MessageOuterClass.Message> {
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
//        ctx.
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageOuterClass.Message message) throws Exception {
        MessageOuterClass.Message.MsgType msgType = message.getMsgType();
        switch (msgType) {
            case login:
                OnlineHelp.add(message.getUserId() + "", channelHandlerContext.channel());
                channelHandlerContext.writeAndFlush(ResultWrapper.simpleMsg(message.getUserId(),"login success"));
                OnlineHelp.publishMsg(message.getUserId()
                        , "用户:" + message.getUserId() + "已上线，当前在线人数为：" + OnlineHelp.size());
                break;
//            case heart:
//                break;
            case sendTxt:
                OnlineHelp.publishMsg(message.getUserId(), message.getInfo());
                break;
            case logout:
                OnlineHelp.remove(message.getUserId() + "");
                break;
            default:
                break;
        }
    }
}
